package org.m2acsi.boundary;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;

@Stateless
public class MongoConnection {

    private static String dataServerName = "localhost";
    private static int portNumber = 27017;
    private static String dataBaseName = "imageDB";
    private static DB db = null;

//    public MongoConnection() {
//        try {
//            Mongo mongo = new Mongo(dataServerName, portNumber);
//            this.db = mongo.getDB(dataBaseName);
//        } catch (UnknownHostException ex) {
//            Logger.getLogger(MongoConnection.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (MongoException ex) {
//            Logger.getLogger(MongoConnection.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
//
//    public DB getDb() {
//        return db;
//    }
    public static DB getMongoConnection() {
        if (db == null) {
            Mongo mongo = null;
            try {
                mongo = new Mongo(dataServerName, portNumber);
            } catch (UnknownHostException ex) {
                Logger.getLogger(MongoConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MongoException ex) {
                Logger.getLogger(MongoConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
            db = mongo.getDB(dataBaseName);
        }
        return db;
    }

    public static void initializeMongoParams(String dataServerNameParam, int portNumberParam, String dataBaseNameParam) {
        dataServerName = dataServerNameParam;
        portNumber = portNumberParam;
        dataBaseName = dataBaseNameParam;
    }
}
